﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SgAutoArchiveBrrDal.Linq
{
   public  class T_USER_Dal
    {
        public static T_USER Get_LoginUser(string name, string pwd)
        {
            string sMd5Pwd = SgArchiveCommons.SGText.MD5(pwd);
            try
            {
               SgAutoArchiveBrrDal.SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                return conn.T_USER.Select(c => c).Where(c => c.U_NAME == name && c.U_PWD == sMd5Pwd).Single();
            }
            catch(Exception ex) { return null; }
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static int Add_User(T_USER en)
        {
            SgAutoArchiveBrrDal.SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            try
            {
                if (conn.T_USER.Select(c => c).Where(c => c.U_NAME == en.U_NAME).Count() > 0)
                    return 0;
                conn.T_USER.Add(en);
                conn.SaveChanges();
                return en.U_ID;
            }
            catch { return -1; }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static int Modify_User(T_USER en)
        {
            SgAutoArchiveBrrDal.SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            try
            {
                if (conn.T_USER.Select(c => c).Where(c => c.U_NAME == en.U_NAME && c.U_ID != en.U_ID).Count() > 0)
                    return 0;
                var query = conn.T_USER.Select(c => c).Where(c => c.U_ID == en.U_ID);
                foreach(var n in query)
                {
                    n.U_D01 = en.U_D01;
                    n.U_D02 = en.U_D02;
                    n.U_I01 = en.U_I01;
                    n.U_I02 = en.U_I02;
                    n.U_NAME = en.U_NAME;
                    ///n.U_PWD = en.U_PWD;
                    n.U_RNAME = en.U_RNAME;
                    n.U_STATUS = en.U_STATUS;
                    n.U_V01 = en.U_V01;
                    n.U_V02 = en.U_V02;
                    n.U_V03 = en.U_V03;
                }
                conn.SaveChanges();
                return en.U_ID;
            }
            catch { return -1; }
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        public static List<T_USER> Get_User()
        {
            SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            try
            {
                return conn.T_USER.Select(c => c).OrderBy(c => c.U_ID).ToList();
            }
            catch { return null; }
        }

        /// <summary>
        /// 重置用户密码
        /// </summary>
        /// <returns></returns>
        public static int Ref_UserPassword(int id)
        {
            string sMd5Pwd = SgArchiveCommons.SGText.MD5("123456");
            try
            {
                SgAutoArchiveBrrDal.SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_USER.Select(c => c).Where(c => c.U_ID == id);
                foreach (var n in query)
                    n.U_PWD = sMd5Pwd;
                conn.SaveChanges();
                return 1;
            }
            catch { return -1; }
        }

        /// <summary>
        /// 禁止用户登录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Set_UserNotLogin(int id)
        {
            try
            {
                SgAutoArchiveBrrDal.SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_USER.Select(c => c).Where(c => c.U_ID == id);
                foreach (var n in query)
                    n.U_STATUS = 0;
                conn.SaveChanges();
                return 1;
            }
            catch { return -1; }
        }
    }
}
